#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<windows.h>
#include<QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("天下第一剑");
//    HWND hwnd = FindWindow(L"天下第一剑",L"天下第一剑");
//    qDebug()<<hwnd;
    EnumWindows([](HWND hwnd, LPARAM lParam)->WINBOOL{
        qDebug() <<hwnd;
        WCHAR title[100];
        GetWindowText(hwnd,title,100);
        qDebug() << QString::fromWCharArray(title);
    },0);
    WCHAR title[100];
    GetWindowText((HWND)this->winId(),title,100);
    qDebug() << QString::fromWCharArray(title);
    auto wndProc = [](HWND hwnd,
            UINT uMsg,
            WPARAM p1,
            LPARAM p2)->LRESULT{
        //lambda的局部变量必须定义在最前面否则报错，
        //是不是与switch有关？

            PAINTSTRUCT ps;
            RECT rect;
            HDC hdc;
            switch (uMsg) {
                case WM_PAINT:


                   hdc = BeginPaint(hwnd,&ps);

                   GetClientRect(hwnd,&rect);
                   DrawText(hdc,L"天下无敌的 天剑术",-1,&rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER);
                   EndPaint(hwnd,&ps);
                   break;
                case WM_QUIT:
                    PostQuitMessage(0);
                    break;
                case WM_DESTROY:
                    PostQuitMessage(0);
                    break;
                default:
                    break;

            }
            return DefWindowProc(hwnd, uMsg, p1, p2);
        };
    //有点问题这个强转 lambda => funptr => long
    SetWindowLongPtr((HWND)this->winId(),GWLP_WNDPROC,
                     reinterpret_cast<LONG_PTR>(static_cast<LRESULT(*)(HWND, UINT, WPARAM, LPARAM)>(wndProc)));
    std::array<TCHAR, 256> className;
    GetClassName((HWND)this->winId(), className.data(), className.size());
    qDebug() << QString::fromWCharArray(className.data());//"Qt5QWindowIcon"
}

MainWindow::~MainWindow()
{
    delete ui;
}

